package com.block.api.controller.source;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.service.LogFactory;
import com.block.source.service.ISourceHandleCheckUserService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;
import java.util.Map;

/**
 * 全流程
 *
 */
@RestController
@RequestMapping("apisource/apis")
public class SourceApiSDataController {

    @Autowired
    ISourceHandleCheckUserService sourceHandleCheckUserService;

    private static final Logger log = LogFactory.getSourceLog();

    /**
     * 线索全流程撞库
     */
    @RequestMapping(value = "/checkUser/{channelCode}", method = {RequestMethod.POST})
    public String sourceCheckUser(HttpServletRequest request,
                            HttpServletResponse httpServletResponse,
                            @PathVariable("channelCode") String channelCode) {

        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            String paramContent = IoUtil.read(request.getInputStream(), Charset.defaultCharset());
            String ip= ServletUtil.getClientIP(request);

            // 线索api 撞库
            Long starttime = System.currentTimeMillis();
            JSONObject returnObject = sourceHandleCheckUserService.apiCheckUser(channelCode, parameterMap, paramContent, ip);
            Long endtime = System.currentTimeMillis();
            String message = StrUtil.format("【线索api撞库】返回数据展示【channelCode】{}【exectime】{}【returnObject】{}", channelCode, endtime-starttime, JSON.toJSONString(returnObject));
            log.info(message);
            return returnObject.toJSONString();
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return "撞库操作异常";
    }

    /**
     * 线索全流程进件
     */
    @RequestMapping(value = "/registerUser/{channelCode}", method = {RequestMethod.POST})
    public String registerUser(HttpServletRequest request,
                               HttpServletResponse httpServletResponse,
                               @PathVariable("channelCode") String channelCode) {
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            String paramContent = IoUtil.read(request.getInputStream(), Charset.defaultCharset());
            String ip= ServletUtil.getClientIP(request);
            // 线索api 进件
            JSONObject returnObject = sourceHandleCheckUserService.apiRegisterUser(channelCode, parameterMap, paramContent, ip);
            String message = StrUtil.format("【线索api进件】返回数据展示【channelCode】{}【returnObject】{}", channelCode, JSON.toJSONString(returnObject));
            log.info(message);
            return returnObject.toJSONString();
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return "撞库操作异常";
    }



}
